import cv2


# 将num转化为5位int数组
def get_digits(num):
    try:
        num = int(num)
        if num < 0 or num >= 100000:
            raise ValueError("")
        num_str = str(num).zfill(5)
        digit = [d for d in num_str]
        return digit
    except ValueError as e:
        print(f"发生了错误：{e}")
        return None


# 定义调整后的视频分辨率
width = 384
height = 288
# 转换完成后一共c帧
c = 0

input_file = "190516_06_AZ-LAGOA-30.mp4"
output_file = "gerenjianjie.mjpeg"

cap = cv2.VideoCapture(input_file)

with open(output_file, "wb") as f:
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        # 重新调整每一帧的大小
        frame = cv2.resize(frame, (width, height))
        retval, buffer = cv2.imencode('.jpg', frame)
        len1 = len(buffer)
        print("大小：", len1)
        digits = get_digits(len1)
        # 写入大小
        for i in range(5):
            b = bytes([ord(digits[i])])
            f.write(b)
        # 写入每帧图像
        f.write(buffer.tobytes())
        c = c + 1

cap.release()
print("视频一共：", c, "帧")
